package com.wxl.common.wiget

import android.content.Context
import android.graphics.PointF
import android.util.DisplayMetrics
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView

/**
 * create file time : 2021/3/2
 * create user : wxl
 * subscribe :
 */
class AutoScrollLayoutManager : LinearLayoutManager{


    @JvmOverloads
    constructor(context: Context) :super(context)

    override fun smoothScrollToPosition(
        recyclerView: RecyclerView?,
        state: RecyclerView.State?,
        position: Int
    ) {
        val  mLinearSmoothScroller = recyclerView?.context?.let { AutoScrollLinearSmoothScroller(it) }
        mLinearSmoothScroller?.targetPosition = position
        startSmoothScroll(mLinearSmoothScroller)
    }


   inner class AutoScrollLinearSmoothScroller : LinearSmoothScroller{

        @JvmOverloads
        constructor(context: Context) :super(context)

        override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
            return this@AutoScrollLayoutManager.computeScrollVectorForPosition(targetPosition)
        }

       override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics?): Float {
           return 15 / displayMetrics?.density!!
       }

    }
}